package com.wyl.template.config.shiro.filter;

import org.apache.shiro.web.filter.authc.UserFilter;
import org.springframework.http.HttpStatus;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: wyl
 * @Date: 2021/4/29
 * @Description:
 * 1、shiro 默认的 user 过滤器会在认证失败情况下重定向到登录页面；
 * 2、返回 401，由前端根据状态码自动跳转
 */
public class MyUserFilter extends UserFilter {

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
        return false;
    }
}
